import config from '@/config'
const PREFIX = `UIN_APP_${config.storePrefix}`

class LocStore<T> {
  constructor(
    public name: string,
    public defaultValue: T //默认值
  ) {}

  get keyName() {
    return PREFIX + this.name
  }

  get(): T {
    const value = uni.getStorageSync(this.keyName)
    if (typeof this.defaultValue !== 'string') {
      return (value && JSON.parse(value)) || this.defaultValue
    }
    return value ?? this.defaultValue
  }

  set(data: T) {
    let _value = data || ''
    if (typeof _value === 'object') {
      _value = JSON.stringify(_value)
    }
    uni.setStorageSync(this.keyName, _value)
  }

  remove() {
    uni.removeStorageSync(this.keyName)
  }
}

export default LocStore
